Skip to main content

JSON module (json)

Start here​

bring json lets you build, parse, and serialize JSON data. The module provides a json::JsonNode class with a fluent API for constructing nested objects and arrays.

A tiny program​

bring json

var obj: json::JsonNode = json::newObject()
obj.put("name", json::newString("FlowWing"))
obj.put("year", json::newNumber(2024))
obj.put("active", json::newBool(true))

var jsonStr: str = json::stringify(obj)
println(jsonStr)

Output:

{"name": "FlowWing", "year": 2024, "active": true}

Building JSON​

Create nodes with the factory functions, then chain calls to build your structure:

Objects​

bring json

var user: json::JsonNode = json::newObject()
  .put("name", json::newString("Alice"))
  .put("age", json::newNumber(30))

var address: json::JsonNode = json::newObject()
  .put("city", json::newString("New York"))
  .put("zip", json::newString("10001"))

user.put("address", address)
println(json::stringify(user))

Arrays​

bring json

var arr: json::JsonNode = json::newArray()
  .push(json::newNumber(1))
  .push(json::newNumber(2))
  .push(json::newNumber(3))

println(json::stringify(arr))

/; Access by index
println("Index 1: ", arr.getAt(1).numVal)

Parsing JSON​

Parse a JSON string into a JsonNode tree:

bring json

var raw: str = "{\"x\":10,\"y\":[1,2,3],\"ok\":true}"
var root: json::JsonNode = json::parse(raw)

println("x = ", root.get("x").numVal)
println("y[0] = ", root.get("y").getAt(0).numVal)

Note on string literals: Flow-Wing uses double quotes for strings. Escape inner double quotes with backslash when embedding JSON: "{\"key\":\"val\"}".

Factory functions​

FunctionReturnsDescription
json::newObject()json::JsonNodeCreates an empty JSON object {}
json::newArray()json::JsonNodeCreates an empty JSON array []
json::newString(s)json::JsonNodeCreates a JSON string node
json::newNumber(n)json::JsonNodeCreates a JSON number node
json::newBool(b)json::JsonNodeCreates a JSON boolean node
json::newNull()json::JsonNodeCreates a JSON null node

Serialization & parsing​

FunctionReturnsDescription
json::stringify(node)strSerializes a node to a JSON string. Strings and keys are escaped, so the result is always valid JSON
json::parse(str)json::JsonNodeParses a JSON string into a node tree
json::quote(s)strs as a JSON string literal: in quotes, with ", \ and control characters escaped

Accessing values​

Property / MethodType / ReturnsDescription
.strValstrString value of a node
.numValdeciNumeric value of a node
.boolValboolBoolean value of a node
.get(key)json::JsonNodeGet value by key in an object
.getAt(index)json::JsonNodeGet item by index in an array (out-of-bounds returns null)
.put(key, val)json::JsonNodeSet/update a key in an object (returns self)
.push(val)json::JsonNodeAppend to an array (returns self)

Supported JSON features​

  • Nested structures — objects inside objects, arrays inside arrays
  • Method chaining — .put().put().push() style
  • Key overwriting — calling .put(key, ...) on an existing key updates it
  • Escaped characters — json::parse reads every JSON escape (\n, \t, \r, \b, \f, \", \\, \/, and \uXXXX, including surrogate pairs); json::stringify writes them back
  • UTF-8 text — kept exactly as it is, in both directions
  • Malformed JSON — json::parse() on invalid input returns an empty object {}

Source & tests (if you have the repository)​

WhatWhere
Module sourcefw-modules/json_module/json-module.fg
Integration fixturestests/fixtures/LatestTests/JsonModuleTests/